/*----------------------------------------------------------------------------*\
| /gamemode/fatigue.lua                | fatigue system                  | SRV |
\*----------------------------------------------------------------------------*/

local fatigue = {}

local meta = FindMetaTable( "Player" )

function meta:SetFatigue( amount )
	self.MyFatigue = math.Clamp( amount, 0, ZDayConfig["MaxFatigue"] )
end

function meta:Fatigue( amount )
	self.MyFatigue = math.Clamp( self.MyFatigue + amount, 0, ZDayConfig["MaxFatigue"] )
end

function meta:GetFatigue()
	return self.MyFatigue
end

function fatigue.ResetPlayer( ply )
	ply.ShouldRun = false
	ply.Running = false
	ply:SetFatigue( ZDayConfig["MaxFatigue"] )
end
hook.Add( "PlayerInitialSpawn", "fatigue.ResetPlayer", fatigue.ResetPlayer )

local prevFatigue = 0

function fatigue.Tick()
	for _, ply in pairs( player.GetAll() ) do
		-- Remember that we have to double the fatigue values here because it's going to run at half the speed on the server.  -BLaNK.
		
		if ( ply:KeyDown( IN_SPEED ) && ( ply:KeyDown( IN_FORWARD )	|| ply:KeyDown( IN_BACK ) || ply:KeyDown( IN_MOVELEFT ) || ply:KeyDown( IN_MOVERIGHT ) ) ) || ply:GetMoveType() == MOVETYPE_LADDER then	
			ply:Fatigue( -4 )
			ply.ShouldRun = true
		else
			if ( !ply:KeyDown( IN_FORWARD ) and !ply:KeyDown( IN_BACK ) and !ply:KeyDown( IN_MOVELEFT ) and !ply:KeyDown( IN_MOVERIGHT ) and ply:GetMoveType() != MOVETYPE_LADDER ) then
				ply:Fatigue( 4 )
			end
			ply.ShouldRun = false
			if ply:Crouching() && ply:IsOnGround() then
				ply:Fatigue( 6 )
			end
		end
		
		local Fatigue = ply:GetFatigue() -- Store a copy after it's been edited to save function calls.
		
		if Fatigue <= 5 then
			ply.ShouldRun = false
		end
			
		if Fatigue != prevFatigue then
			umsg.Start( "SyncFatigue", ply )
				umsg.Short( Fatigue )
			umsg.End()
			prevFatigue = Fatigue -- Save us from sending the same number over and over.
		end
	end
end
timer.Create( "fatigue.Tick", 0.2, 0, fatigue.Tick )

function ZDSetupMove(ply, move)
	if ( not ply:IsValid() or not ply:Alive() ) then
		return;
	end
	if ( ply:GetMoveType() == MOVETYPE_LADDER ) then
		move:SetMaxClientSpeed( 20 );
		ply.Running = false;
		return true;
	end
	if ( ply:Crouching() ) then
		if ply.ShouldRun == true then
			move:SetMaxClientSpeed( 240 );
			ply.Running = true
			return true;
		end
	
		if ply.ShouldRun == false then
			move:SetMaxClientSpeed( 134 );
			ply.Running = false
			return true;
		end
	else
		if ply.ShouldRun == true && ply.Running == false then
			move:SetMaxClientSpeed( 190 );
			ply.Running = true
			return true;
		end
	
		if ply.ShouldRun == false then
			move:SetMaxClientSpeed( 92 );
			ply.Running = false
			return true;
		end
	end
end
hook.Add( "SetupMove", "ZDSetupMove", ZDSetupMove );

function Jump(ply,key)
	if key == IN_JUMP then
		if ply:IsOnGround() and ply:GetFatigue() > ZDayConfig["Fatigue_Jump"] then
			ply:Fatigue(-ZDayConfig["Fatigue_Jump"])
		else
			ply:SetVelocity( Vector( 0, 0, -40 ) ) // cancel the jump
			return true
		end
	end
end
hook.Add( "KeyPress", "Jump", Jump ) 